// pages/push/deal/index.js
import wxRequest from '../../../utils/wxRequest.js';
import utils from '../../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wx_id: null, //区别添加、修改
    wx_arr: null, //存放修改数据
    visibleSelectLogic: false, //显示选择框
    group_array: ['广州男科'],
    group_index: 0,
    status_array: [{
      id: 1,
      name: '正常'
    }, {
      id: 2,
      name: '暂封'
    }, {
      id: 3,
      name: '永封'
    }, {
      id: 4,
      name: '禁用'
    }],
    status_index: 0,
    start_time: '00:00', //上线时间
    end_time: '24:00', //下线时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.getGroup();

    if (options && options.wx_id) {
      that.setData({
        wx_id: options.wx_id
      });

      wx.setNavigationBarTitle({
        title: '微信修改'
      })

      var map = {
        'id': options.wx_id
      };
      wxRequest.getRequest('/toolkit/wechat/edit', map).then(res => {
        if (res.code == 0) {
          for (var i = 0; i < that.data.group_array.length; i++) {
            if (that.data.group_array[i].id == res.data.result.group_id){
              that.setData({
                group_index:i
              })
            }
          }
          for (var i = 0; i < that.data.status_array.length; i++) {
            if (that.data.status_array[i].id == res.data.result.status) {
              that.setData({
                status_index: i
              })
            }
          }

          that.setData({
            wx_arr: res.data.result,
            start_time: res.data.result.last_time, //上线时间
            end_time: res.data.result.next_time, //下线时间
          })
        }
      }).catch(error => {})
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  bindStartTimeChange(e) { //上线时间
    this.setData({
      start_time: e.detail.value
    })
  },
  bindEndTimeChange(e) { //下线时间
    this.setData({
      end_time: e.detail.value
    })
  },
  // 选择分组
  bindPickerChange: function(e) {
    this.setData({
      group_index: e.detail.value
    })
  },
  //选择状态
  statusChange: function(e) {
    this.setData({
      status_index: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    that.getGroup();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  //获取分组
  getGroup: function() {
    var that = this;
    wxRequest.getRequest('/toolkit/wechat/create').then(res => {
      that.setData({
        group_array: res.data.result,
        status_array: res.data.status_arr
      })
    }).catch(error => {})
  },

  //提交
  submitFn: function(e) {
    var that = this;
    if (that.data.wx_id == null) {
      //新增
      var map = {
        'wx_num': e.detail.value.wx_num,
        'wx_nike': e.detail.value.wx_nike,
        'group_id': that.data.group_array[that.data.group_index].id,
        'phone': e.detail.value.phone,
        'rand_num': e.detail.value.rand_num,
        'last_time': e.detail.value.last_time,
        'next_time': e.detail.value.next_time,
        'status': that.data.status_array[that.data.status_index].id
      }
      wxRequest.getRequest('/toolkit/wechat/save', map).then(res => {
        if (res.code == 0) {
          wx.showToast({
            icon: 'success',
            title: res.message,
            duration: 1000,
            complete: function() {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.message,
            duration: 1000
          })
        }
      }).catch(error => {})
    }else{
      //修改
      var map = {
        'id': that.data.wx_id,
        'wx_num': e.detail.value.wx_num,
        'wx_nike': e.detail.value.wx_nike,
        'group_id': that.data.group_array[that.data.group_index].id,
        'phone': e.detail.value.phone,
        'rand_num': e.detail.value.rand_num,
        'last_time': e.detail.value.last_time,
        'next_time': e.detail.value.next_time,
        'status': that.data.status_array[that.data.status_index].id
      }
      wxRequest.getRequest('/toolkit/wechat/update', map).then(res => {
        if (res.code == 0) {
          wx.showToast({
            icon: 'success',
            title: res.message,
            duration: 1000,
            complete: function () {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.message,
            duration: 1000
          })
        }
      }).catch(error => { })
    }
  }

})